package com.videoadmin.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.videoadmin.dao.IPointsCardDAO;
import com.videoadmin.pojo.PointsCard;
import com.videoadmin.utils.HibernateUtil;

public class PointsCardDAOImpl implements IPointsCardDAO {
	
	/* (non-Javadoc)
	 * @see com.video.dao.IPoints_CardDao#saveOrUpdate(com.domain.Points_Card)
	 */
	public void saveOrUpdate(PointsCard card){
		Session s = null;
		Transaction ts = null;
		try {
			s = HibernateUtil.getSession();
			ts = s.beginTransaction();
			s.saveOrUpdate(card);
			ts.commit();
		} catch (HibernateException e) {
			if (ts != null)
				ts.rollback();
			throw e;
		} finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.IPoints_CardDao#delete(com.domain.Points_Card)
	 */
	public void delete(PointsCard card){
		Session s = null;
		Transaction ts = null;
		try {
			s = HibernateUtil.getSession();
			ts = s.beginTransaction();
			s.delete(card);
			ts.commit();
		} catch (HibernateException e) {
			if (ts != null)
				ts.rollback();
			throw e;
		} finally {
			if (s != null)
				s.close();
		}
	}
	/* (non-Javadoc)
	 * @see com.video.dao.IPoints_CardDao#findById(int)
	 */
	public PointsCard findById(int id){
		Session s = null;
		try {
			s = HibernateUtil.getSession();
			PointsCard card = (PointsCard) s.get(PointsCard.class, id);
			return card;
		} finally {
			if (s != null)
				s.close();
		}
	}
	
	public PointsCard findByPassword(String password){
		Session s=null;
		List<PointsCard> list=new ArrayList<PointsCard>();
		PointsCard test=new PointsCard();
		try{
			s = HibernateUtil.getSession();
			list=(List<PointsCard>)s.createSQLQuery("select * from points_card where Password='"+password+"'").addEntity(PointsCard.class).list();
			if(list.size() == 0)
				return null;
			else
			{
				test=list.get(0);
				return test;
			}
		}finally {
			if (s != null)
				s.close();
		}
	}

}
